/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_pwm.h
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the pwm module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-26  |V2.0     |Wang Junxiong|New creat
 *  |2025-08-08  |V2.1.1   |Wang Junxiong|Before enabling PWM1_2, it is necessary to wait for 4 NOP cycles
 *  |2025-08-12  |V2.1.2   |Wang Junxiong|T2 Divider?1, only using a 1:1 division
 *****************************************************************************/

#ifndef KF8A100FXX_PWM_H
#define KF8A100FXX_PWM_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx.h"
/******************************************************************************
 *                      Macro or Inline
 *****************************************************************************/

/**
 * @brief pwm channel switch
 * 0: disable, 1:enable
 * @note Before enabling PWM1_2, it is necessary to wait for 4 NOP cycles, for example:
 * _NOP();_NOP();_NOP();_NOP();
 * PWM10_ENABLE(1);
 */
#define PWM10_ENABLE(x) (PWM10ON = (x))
#define PWM11_ENABLE(x) (PWM11ON = (x))
#define PWM12_ENABLE(x) (PWM12ON = (x))
#define PWM13_ENABLE(x) (PWM13ON = (x))
#define PWM20_ENABLE(x) (PWM20ON = (x))
#define PWM21_ENABLE(x) (PWM21ON = (x))
#define PWM22_ENABLE(x) (PWM22ON = (x))
#define PWM23_ENABLE(x) (PWM23ON = (x))
#define PWM30_ENABLE(x) (PWM30ON = (x))
#define PWM31_ENABLE(x) (PWM31ON = (x))
#define PWM32_ENABLE(x) (PWM32ON = (x))
#define PWM33_ENABLE(x) (PWM33ON = (x))

/**
 * @brief pwm module disable/enable
 * 0: disable, 1:enable
 *
 */
#define PWM1_2_ENABLE(x) (T1ON = (x))
#define PWM2_ENABLE(x)   (T1ON = (x))
#define PWM3_ENABLE(x)   (T3ON = (x))
#define PWM5_ENABLE(x)   (T2ON = (x))

/*****************************************************************************
 *                      Typedef Definitions
 *****************************************************************************/

/**
 * @brief PWM1/2 prescale
 */
typedef enum
{
    PWM1_2_PRESCALER_1 = 0u,
    PWM1_2_PRESCALER_2 = 16u,
    PWM1_2_PRESCALER_4 = 32u,
    PWM1_2_PRESCALER_8 = 48u
} Pwm1_2_PrescalerType;

/**
 * @brief PWM1/2 clock source
 */
typedef enum
{
    /** @brief High frequency internal crystal: 16MHz */
    PWM1_2_SOURCE_INTHF = 1u,
    /** @brief System clock/4 */
    PWM1_2_SOURCE_SCLKDIV4 = 0u
} Pwm1_2_SourceType;

/**
 * @brief Timer 3 prescale
 */
typedef enum
{
    PWM3_PRESCALER_1 = 0u,
    PWM3_PRESCALER_2 = 16u,
    PWM3_PRESCALER_4 = 32u,
    PWM3_PRESCALER_8 = 48u
} Pwm3_PrescalerType;

/**
 * @brief Timer 3 clock source
 */
typedef enum
{
    /** @brief Internal High Frequency Crystal */
    PWM3_SOURCE_INTHF = 0u,
    /** @brief Internal low frequency crystal */
    PWM3_SOURCE_INTLF = 2u,
    /** @brief External High Frequency Crystal */
    PWM3_SOURCE_EXTHF = 4u,
} Pwm3_SourceType;

/**
 * @brief PWM5 prescale
 */
typedef enum
{
    PWM5_PRESCALER1_1  = 0u,
} Pwm5_PrescalerType;

/**
 * @brief PWM5 clock source
 */
typedef enum
{
    /** @brief Count up, interrupt flag on overflow */
    PWM5_COUNT_UP_U = 0u,
} Pwm5_CountDirectionType;

/**
 * @brief PWM5 pwm mode
 */
typedef enum
{
    PWM5_PWM_DISABLE                   = 0u,
    PWM5_PWM_P5A_P5C_HIGH_P5B_P5D_HIGH = 12u,
    PWM5_PWM_P5A_P5C_HIGH_P5B_P5D_LOW  = 13u,
    PWM5_PWM_P5A_P5C_LOW_P5B_P5D_HIGH  = 14u,
    PWM5_PWM_P5A_P5C_LOW_P5B_P5D_LOW   = 15u,
} Pwm5_PwmModeType;

/**
 * @brief PWM5 CH1 mode
 */
typedef enum
{
    PWM5_CH1_SINGLE_OUTPUT              = 0u,
    PWM5_CH1_FULL_BRIDGE_FORWARD_OUTPUT = 64u,
    PWM5_CH1_HALF_BRIDGE_OUTPUT         = 128u,
    PWM5_CH1_FULL_BRIDGE_REVERSE_OUTPUT = 192u,
} Pwm5_CH1_PwmModeType;
/**
 * @brief PWM5 CH2 mode
 */
typedef enum
{
    PWM5_CH2_SINGLE_OUTPUT      = 0u,
    PWM5_CH2_HALF_BRIDGE_OUTPUT = 1u,
} Pwm5_CH2_PwmModeType;
/**
 * @brief PWM5 CH3 mode
 */
typedef enum
{
    PWM5_CH3_SINGLE_OUTPUT      = 0u,
    PWM5_CH3_HALF_BRIDGE_OUTPUT = 1u,
} Pwm5_CH3_PwmModeType;
/******************************************************************************
 *                      Export Variables
 *****************************************************************************/

/******************************************************************************
 *                      Export Functions
 *****************************************************************************/

/**
 * @brief Initialize T1 to 8-bit PWM mode
 *
 * @param Div PWM prescale
 * @param Period1 pwm 1 period value
 * @param Period2 pwm 2 period value
 */
void Pwm1_2_8BitModuleInit(Pwm1_2_PrescalerType Div, uint8_t Period1, uint8_t Period2);

/**
 * @brief Initialize PWM10 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm10_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM11 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm11_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM12 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm12_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM13 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm13_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM20 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm20_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM21 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm21_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM22 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm22_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize PWM23 channel
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm23_8BitChannelEnable(uint8_t DutyValue);

/**
 * @brief Initialize T1 to 16-bit PWM mode
 *
 * @param Div PWM prescale
 * @param Period Periodic value: n*256
 */
void Pwm2_16BitModuleInit(Pwm1_2_PrescalerType Div, uint8_t Period);

/**
 * @brief Initialize PWM20 channel
 *
 * @param DutyValue Duty value
 */
void Pwm20_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM21 channel
 *
 * @param DutyValue Duty value
 */
void Pwm21_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM22 channel
 *
 * @param DutyValue Duty value
 */
void Pwm22_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM23 channel
 *
 * @param DutyValue Duty value
 */
void Pwm23_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize T3 to 16-bit PWM mode
 *
 * @param Div PWM prescale
 * @param Period Periodic value
 */
void Pwm3_16BitModuleInit(Pwm3_PrescalerType Div, uint16_t Period);

/**
 * @brief Initialize PWM30 channel
 *
 * @param DutyValue Duty value
 */
void Pwm30_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM31 channel
 *
 * @param DutyValue Duty value
 */
void Pwm31_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM32 channel
 *
 * @param DutyValue Duty value
 */
void Pwm32_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize PWM33 channel
 *
 * @param DutyValue Duty value
 */
void Pwm33_16BitChannelEnable(uint16_t DutyValue);

/**
 * @brief Initialize T1 to PWM mode
 *
 * @param Mode PWM mode
 * @param DeadZone PWM dead zone
 * @param Div prescale
 * @param Period PP5H, PP5L
 */
void Pwm5_16BitModuleInit(Pwm5_PwmModeType Mode, uint8_t DeadZone, Pwm5_CountDirectionType Direction,
                          Pwm5_PrescalerType Div, uint16_t Period);

/**
 * @brief Initialize PWM5 channel 1
 *
 * @param Mode PWM5 ch1 mode
 * @param DutyValue Duty value
 */
void Pwm5_16BitCh1Enable(Pwm5_CH1_PwmModeType Mode, uint16_t DutyValue);

/**
 * @brief Initialize PWM5 channel 2
 *
 * @param Mode PWM5 ch2 mode
 * @param DutyValue Duty value
 */

void Pwm5_16BitCh2Enable(Pwm5_CH2_PwmModeType Mode, uint16_t DutyValue);
/**
 * @brief Initialize PWM5 channel 3
 *
 * @param Mode PWM5 ch3 mode
 * @param DutyValue Duty value
 */
void Pwm5_16BitCh3Enable(Pwm5_CH3_PwmModeType Mode, uint16_t DutyValue);

/**
 * @brief Set T1 period
 *
 * @param Period1 pwm 1 period value
 */
void Pwm1_8BitSetPeriodValue(uint8_t Period1);

/**
 * @brief Set T1 period
 *
 * @param Period2 pwm 2 period value
 */
void Pwm2_8BitSetPeriodValue(uint8_t Period2);

/**
 * @brief Set PWM10 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm10_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM11 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm11_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM12 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm12_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM13 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm13_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM20 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm20_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM21 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm21_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM22 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm22_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set PWM23 duty cycle value
 *
 * @param DutyValue Duty Cycle Value
 */
void Pwm23_8BitSetDutyValue(uint8_t DutyValue);

/**
 * @brief Set T1 to 16-bit PWM mode
 *
 * @param Period Periodic value
 */
void Pwm2_16BitSetPeriodValue(uint8_t Period);

/**
 * @brief Set PWM20 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm20_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM21 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm21_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM22 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm22_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM23 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm23_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set T3 to 16-bit PWM mode
 *
 * @param Period Periodic value
 */
void Pwm3_16BitSetPeriodValue(uint16_t Period);

/**
 * @brief Set PWM30 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm30_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM31 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm31_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM32 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm32_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM33 duty cycle value
 *
 * @param DutyValue Duty value
 */
void Pwm33_16BitSetDutyValue(uint16_t DutyValue);

/**
 * @brief Set T1 to PWM mode
 *
 * @param Period PP5H, PP5L
 */
void Pwm5_16BitSetPeriodValue(uint16_t Period);

/**
 * @brief Set PWM5 duty cycle value 1
 *
 * @param DutyValue Duty value
 */
void Pwm5_16BitCh1SetDutyValue(uint16_t DutyValue);

/**
 * @brief Set PWM5 duty cycle value 2
 *
 * @param DutyValue Duty value
 */

void Pwm5_16BitCh2SetDutyValue(uint16_t DutyValue);
/**
 * @brief Set PWM5 duty cycle value 3
 *
 * @param DutyValue Duty value
 */
void Pwm5_16BitCh3SetDutyValue(uint16_t DutyValue);

/**
 * @brief Set Pwm 5 dead zone
 *
 * @param DeadZone PWM dead zone
 */
void Pwm5_16BitSetDeadZone(uint8_t DeadZone);

/**
 * @brief PWM1_2 deinitialization
 *
 */
void Pwm1_2_DeInit(void);

/**
 * @brief PWM2 deinitialization
 *
 */
void Pwm2_DeInit(void);

/**
 * @brief PWM3 deinitialization
 *
 */
void Pwm3_DeInit(void);

/**
 * @brief PWM5 deinitialization
 *
 */
void Pwm5_DeInit(void);

#ifdef __cplusplus
}
#endif

#endif
/* EOF */
